package com.bw.day03.fg

import android.content.Context
import android.os.Bundle
import android.os.Handler
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.lifecycle.observe
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.net.entity.Roll
import com.bw.base.net.mvvm.BaseFragment
import com.bw.day03.R
import com.bw.day03.adapter.GoodsAdapter
import com.bw.day03.adapter.KanJiaAdapter
import com.bw.day03.adapter.MenuAdapter
import com.bw.day03.adapter.MoneyAdapter
import com.bw.day03.adapter.NewsAdapter
import com.bw.day03.adapter.QuanAdapter
import com.bw.day03.adapter.TeamAdapter
import com.bw.day03.databinding.FragmentHomeBinding
import com.bw.day03.util.GlideUtlis
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.loader.ImageLoader
import java.util.Timer
import java.util.TimerTask

class HomeFragment : BaseFragment<FragmentHomeBinding,HomeViewModel>() {
    val imgs= mutableListOf<String>()
    lateinit var menuAdapter: MenuAdapter
    lateinit var newsAdapter: NewsAdapter
    lateinit var timer: Timer
    val handler=Handler()
    var position=0
    lateinit var moneyAdapter: MoneyAdapter
    lateinit var goodsAdapter: GoodsAdapter
    lateinit var quanAdapter: QuanAdapter
    lateinit var kanJiaAdapter: KanJiaAdapter
    lateinit var teamAdapter: TeamAdapter
    var type=4
    var page=1
    override fun initView() {
        viewModel.index()
        teamAdapter= TeamAdapter()
        binding.rvTeam.adapter=teamAdapter
        binding.rvTeam.layoutManager=LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL,false)
        viewModel.team(20,1)
        menuAdapter=MenuAdapter()
        binding.rvMenu.adapter=menuAdapter
        binding.rvMenu.layoutManager=GridLayoutManager(requireContext(),5)
        newsAdapter=NewsAdapter()
        binding.rvNews.adapter=newsAdapter
        binding.rvNews.layoutManager=LinearLayoutManager(context)
        moneyAdapter=MoneyAdapter()
        binding.rvMoney.adapter=moneyAdapter
        binding.rvMoney.layoutManager=GridLayoutManager(context,4)
        timer=Timer()
        timer.schedule(object :TimerTask(){
            override fun run() {
                handler.post {
                    binding.rvNews.scrollToPosition(position)
                    position++
                    if (position==newsAdapter.data.size){
                        position=0
                    }
                }
            }

        },0,2000)

        goodsAdapter=GoodsAdapter()
        binding.rvGoods.adapter=goodsAdapter
        binding.rvGoods.layoutManager=GridLayoutManager(context,2)
        viewModel.indexGoods(type,5,page)
        kanJiaAdapter= KanJiaAdapter()
        binding.rvKanjia.adapter=kanJiaAdapter
        binding.rvKanjia.layoutManager= LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL,false)
         viewModel.kanjia()

        moneyAdapter.setOnItemClickListener { adapter, view, position ->
            type=moneyAdapter.data[position].type.toInt()
            moneyAdapter.updateType(type)
            page=1
            viewModel.indexGoods(type,5,page)
        }

        binding.smart.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page=1
                viewModel.indexGoods(type,5,page)
                binding.smart.finishRefresh()
            }
            



            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++

                viewModel.indexGoods(type,5,page)
                binding.smart.finishLoadMore()
            }
        })

        binding.ivSearch.setOnClickListener {
            ARouter.getInstance().build("/app/SearchActivity").navigation()
        }
        quanAdapter=QuanAdapter()
        binding.rvQuan.adapter=quanAdapter
        binding.rvQuan.layoutManager=LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)
        viewModel.quan("10","1","1")
        quanAdapter.addChildClickViewIds(R.id.quan_status)
        quanAdapter.setOnItemChildClickListener {  adapter, view, position ->
            when(view.id){
                R.id.quan_status->{
                    if (quanAdapter.data[position].isUse){
                        ToastUtils.showLong("已经领取了，不能重复领取")
                    }else{
                        viewModel.quanReceive(quanAdapter.data[position].id)
                        quanAdapter.data[position].isUse=true
                        quanAdapter.notifyDataSetChanged()
                    }
                }
            }
        }
        menuAdapter.setOnItemClickListener { adapter,view,position->

            when(position){
                7->{
                    ARouter.getInstance().build("/app/AddressActivity").navigation()
                }
                9->{
                    ARouter.getInstance().build("/app/QuanActivity").navigation()
                }
            }
        }

        kanJiaAdapter.setOnItemClickListener {adapter,view,position->
            ARouter.getInstance().build("/app/KanjiaActivity").navigation()
        }

        goodsAdapter.setOnItemClickListener {  adapter,view,position->
            ARouter.getInstance().build("/app/XqActivity").withInt("goodsId",goodsAdapter.data[position].id).navigation()
        }


    }

    override fun initData() {
       viewModel.indexSuccess.observe(this) {
           if (it.code == 200) {
               it.data.banner.forEach {
                   imgs.add(it.pic)
               }
               binding.banner.setImages(imgs)
                   .setImageLoader(object :ImageLoader(){
                       override fun displayImage(
                           context: Context,
                           path: Any,
                           imageView: ImageView
                       ) {
                           GlideUtlis.loadNormal(context,path.toString(),imageView)
                       }
                   }).start()
               menuAdapter.data.clear()
               menuAdapter.data.addAll(it.data.menus)
               menuAdapter.notifyDataSetChanged()

               newsAdapter.data.clear()
               newsAdapter.data.addAll(it.data.roll)
               newsAdapter.data.add(Roll(123,"新品限量抢，积分翻两倍","",""))
               newsAdapter.notifyDataSetChanged()

               moneyAdapter.data.clear()
               moneyAdapter.data.addAll(it.data.explosiveMoney)
               moneyAdapter.notifyDataSetChanged()

           } else {
               ToastUtils.showLong(it.message)
           }
       }
        viewModel.goodsSuccess.observe(this){
            if (it.code==200){
                if (page==1){
                    goodsAdapter.data.clear()
                }
                goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
                if (it.data.list.isEmpty()){
                    ToastUtils.showLong("没有加载到更多数据")
                }
            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.quanSuccess.observe(this){
            if (it.code==200){
                quanAdapter.data.clear()
                quanAdapter.data.addAll(it.data)
                quanAdapter.notifyDataSetChanged()
            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.receiveSuccess.observe(this){
            if (it.code==200){
                ToastUtils.showLong("领取成功")
            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.kanjiaSuccess.observe (this){
            if (it.code==200){
                kanJiaAdapter.data.clear()
                kanJiaAdapter.data.addAll( it.data.productList)
                kanJiaAdapter.notifyDataSetChanged()
            }
        }
        viewModel.teamSuccess.observe (this){
            if (it.code==200){
                teamAdapter.data.clear()
                teamAdapter.data.addAll( it.data.list)
                teamAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun getViewModel(): Class<HomeViewModel> = HomeViewModel::class.java

    override fun getLayoutId(): Int =R.layout.fragment_home

}